CREATEINDEX Statement ---------------------------------------------------------------------------- Action Creates an index consisting of one or more columns of an ISAM table. Syntax CREATEINDEX -#-filenumber%, indexname$, unique%, columnname$-, columnname$-... Remarks The CREATEINDEX statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- filenumber% The number used in the OPEN statement to open the table. indexname$ The indexname$ is the name of the index until the index is Argument Description ---------------------------------------------------------------------------- index until the index is explicitly deleted. It follows the ISAM naming conventions (details are provided later in this entry). unique% A non-zero value for unique% indicates the index is unique--no two indexed values can be the same. A value of zero for unique% means the indexed values need not be unique. columnname$ Names the column or columns to be indexed. The name follows the ISAM naming conventions. If more than one column name is given, CREATEINDEX defines an index based on the combination of their values. The argument columnname$ must Argument Description ---------------------------------------------------------------------------- The argument columnname$ must appear in the TYPE statement used when the table was created. When you initially open a table, the current index is the null index. The null index represents the order in which records were added to the file. Once an index has been created, it can be used any number of times until it is deleted from the database. Use SETINDEX to make an index the current index and impose its order on the presentation of records in a table. Note Columns that are arrays, user-defined types, or strings longer than 255 characters cannot be indexed. ISAM names use the following conventions. - They have no more than 30 characters. - They use only alphanumeric characters (A-Z, a-z, 0-9). - They begin with an alphabetic character. - They include no BASIC special characters. See Also DELETEINDEX, GETINDEX$, SETINDEX Example This example uses the CREATEINDEX, SETINDEX and DELETEINDEX statements to index an ISAM file in several ways. It displays records from the file using the GETINDEX$ and BOF functions and the MOVEFIRST, MOVELAST, MOVENEXT, and MOVEPREVIOUS statements. The program uses a file called BOOKS.MDB, the sample ISAM file that SETUP copies to your disk. DEFINT A-Z TYPE BookRec IDNum AS DOUBLE ' Unique ID number for each book. Title AS STRING * 50 ' Book's title. Publisher AS STRING * 50 ' Book's publisher. Author AS STRING * 36 ' Book's author. Price AS CURRENCY ' Book's price. END TYPE DIM Library AS BookRec ' Record structure variable. DIM msgtxt AS STRING CONST Database = "BOOKS.MDB" ' Name of the disk file. CONST TableName = "BooksStock" ' Name of the table. tablenum = FREEFILE ' File number. OPEN Database FOR ISAM BookRec TableName AS tablenum CREATEINDEX tablenum, "A", 0, "Author" CREATEINDEX tablenum, "I", 1, "IDnum" CREATEINDEX tablenum, "T", 0, "Title" CREATEINDEX tablenum, "C", 0, "Price" ' Display static instructions. CLS . LOCATE 13, 30 PRINT "Choose a key." PRINT SPC(9); "Move to."; TAB(49); "Order by. X" PRINT . PRINT SPC(9); "F - First record"; TAB(49); "A - Author" PRINT . PRINT SPC(9); "L - Last record"; TAB(49); "I - ID Number" PRINT . PRINT SPC(9); "N - Next record"; TAB(49); "T - Title" PRINT . PRINT SPC(9); "P - Previous record"; TAB(49); "C - Cost" PRINT . PRINT SPC(9); "Q - Quit"; TAB(49); "X - No order" LOCATE 3, 1. PRINT TAB(37); "Books" PRINT STRING$(80, "-"); VIEW PRINT 5 TO 10 ' Set viewport for displaying records.age. MOVEFIRST tablenum DO ' Display current record. CLS RETRIEVE tablenum, Library PRINT "Author. "; Library.Author; PRINT TAB(49); "ID #"; Library.IDNum PRINT "Title. "; Library.Title PRINT "Publisher. "; Library.Publisher PRINT "Cost. "; Library.Price PRINT SPC(30); msgtxt PRINT STRING$(64, "-"); IF GETINDEX$(tablenum) = "" THEN PRINT STRING$(15, "-"); ELSE PRINT "Index in use. "; GETINDEX$(tablenum); END IF ' Get keystroke from user. validkeys$ = "FLNPQATICX" DO Keychoice$ = UCASE$(INKEY$) LOOP WHILE INSTR(validkeys$, Keychoice$) = 0 OR Keychoice$ = "" msgtxt = "" ' Move to appropriate record, or change indexes. SELECT CASE Keychoice$ CASE "F" MOVEFIRST tablenum CASE "L" MOVELAST tablenum CASE "N" MOVENEXT tablenum IF EOF(tablenum) THEN MOVELAST tablenum BEEP. msgtxt = "** At last record **" END IF CASE "P" MOVEPREVIOUS tablenum IF BOF(tablenum) THEN MOVEFIRST tablenum BEEP. msgtxt = "** At first record **" END IF CASE "Q" EXIT DO CASE ELSE 'User chose an index. VIEW PRINT LOCATE 13, 59. PRINT Keychoice$; VIEW PRINT 5 TO 10 IF Keychoice$ = "X" THEN Keychoice$ = "" SETINDEX tablenum, Keychoice$ MOVEFIRST tablenum END SELECT LOOP ' User wants to quit, so reset viewport, delete indexes and close files. VIEW PRINT DELETEINDEX tablenum, "A" DELETEINDEX tablenum, "I" DELETEINDEX tablenum, "T" DELETEINDEX tablenum, "C" CLOSE END